package buildcode.language.java.command;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;

import buildcode.build.AbstractSingleTableCommandExecuter;
import buildcode.config.EntityExportConfig;
import buildcode.database.DataBaseTable;
import buildcode.language.Type;
import buildcode.language.java.field.JavaEnumField;
import buildcode.language.java.type.JavaEnum;
import buildcode.util.CommonUtil;
import buildcode.util.EnumDictUtil;

public class JavaEnumCommandExecute extends AbstractSingleTableCommandExecuter {

    @Override
    public List<Type> execute(DataBaseTable base, EntityExportConfig config) {
        return base.getColumns().stream().filter(v -> v.getEnumColumnInfo() != null).map(v -> {
            JavaEnum entity = new JavaEnum(base.getCamelCaseName() + CommonUtil.firstUpper(v.getCamelCaseName()) + config.getSuffix(), config.getPackageName());
            entity.setRemark(base.getTableRemark() + v.getColumnName() + config.getSuffix());
            v.getEnumColumnInfo().getEnumValues().forEach((key, val) -> {
                entity.addField(new JavaEnumField(StringUtils.defaultString(EnumDictUtil.getYing(val), val), key, val, v));
            });
            return entity;
        }).collect(Collectors.toList());
    }

}
